package com.yutian.commandapp;

import java.util.Scanner;

/**
 * Hello world!
 */
public class CommnadApp {
    public static void main(String[] args) {
//        if(null == args || args.length < 3){
//            System.out.println("未设置正确的启动参数!");
//        } else {
//            calculate(args);
//        }

        Scanner scan = new Scanner(System.in);
        System.out.println("输入exit结束程序，或继续输入启动参数（空格分隔，如‘1 + 2’）：");
        while (scan.hasNextLine()) {
            // This method may block while waiting for input
            // 判断是否还有输入
            String inputString = scan.nextLine();
            System.out.println("输入为： ["+ inputString + "]");
            if("exit".equals(inputString)){
                System.out.println("程序结束!");
                return;
            }
            String[] inputs = inputString.split(" ");
            if(null == inputs || inputs.length < 3){
                System.out.println("未设置正确的启动参数!");
            }
            calculate(inputs);
            System.out.println("输入exit结束程序，或继续输入启动参数（空格分隔，如‘1 + 2’）：");
        }
        System.out.println("程序结束!");
    }

    /**
     *
     * @param args
     * @return
     */
    private static void calculate(String[] args) {
        System.out.println("启动参数如下：");
        int len = args.length;
        for(int i=0; i<len; i++){
            System.out.println( "第" + i + "个启动参数: ["+ args[i] + "]");
        }
        System.out.println("启动参数进行简单计算：");

        String first = args[0];
        String operator = args[1];
        String second = args[2];

        // TODO 小数计算，需采用BigDecimal
        Double f = Double.valueOf(first);
        Double s = Double.valueOf(second);

        // IntegerOper integerOper = new IntegerOper(f, s);
        BigDecimalOper integerOper = new BigDecimalOper(f, s);
        if("+".equals(operator)){
            System.out.println("加法计算：" + integerOper.add());
        }else if("-".equals(operator)){
            System.out.println("减法计算：" + integerOper.subtract());
        }else if("*".equals(operator)){
            System.out.println("乘法计算：" + integerOper.multiply());
        }else if("/".equals(operator)){
            System.out.println("除法计算：" + integerOper.dividePrecise(3));
        }
    }
}
